<?php

namespace app\selladmin\controller;

use app\selladmin\controller\CommonController;
use app\selladmin\model\Selladmin;
use think\Db;
use think\Validate;
use app\util\File;
use think\Request;
use app\selladmin\model\Sellrole;
use app\selladmin\model\Sellorder;

class MoneyController extends CommonController {
    /*
     * 入驻费列表
     * author xfwed
     * date 2019.01.22
     */

    public function enterMoneyList() {
        $id = session('S_ID');
        $row = Selladmin::where('id', '=', $id)->field('id,level')->find();
        $level = $row['level'];
        if ($level == 0 || $level == 1) {
            $where1['level'] = 2;
        } elseif ($level == 2) {
            $where1['pid'] = $id;
        } elseif ($level == 3) {
            $where1['id'] = $id;
        }
        $selladmin1 = Selladmin::where($where1)->where('status', '=', 1)->select();


        $store_name = input('param.store_name');
        $store_phone = input('param.store_phone');
        $ordnum = input('param.ordnum');
        $adminid = input('param.adminid');
        if ($store_phone) {
            $where['s.store_phone'] = $store_phone;
            $query['store_phone'] = $store_phone;
        }

        if ($store_name) {
            $where['s.store_name'] = array('like', '%' . $store_name . '%');
            $query['store_name'] = $store_name;
        }

        if ($ordnum) {
            $where['so.ord_num'] = $ordnum;
            $query['ordnum'] = $ordnum;
        }

        $ctm_id = session('S_ID');
        $selladmin = Selladmin::where('id', '=', $ctm_id)->field('id,level')->find();
        $level = $selladmin['level'];
        if ($level == 0 or $level == 1) {//超级管理员和总经理
            if ($adminid) {
                $selladminrow = Selladmin::where('id', '=', $adminid)->field('id,level')->find();
                if ($selladminrow['level'] == 2) {
                    $ids = Selladmin::where('pid', '=', $adminid)->column('id');
                    $where['so.sell_id'] = ['in', $ids];
                } elseif ($selladminrow['level'] == 3) {
                    $where['so.sell_id'] = $adminid;
                }
                $query['adminid'] = $adminid;
            }
        } elseif ($level == 2) {//经理
            if ($adminid) {
                $where['so.sell_id'] = $adminid;
                $query['adminid'] = $adminid;
            } else {
                $ids = Selladmin::where('pid', '=', $ctm_id)->column('id');
                $where['so.sell_id'] = ['in', $ids];
            }
        } elseif ($level == 3) {//员工
            $where['so.sell_id'] = $ctm_id;
        }

        $where['so.type'] = ['in', '1,2,3'];
        $query['type'] = '1,2,3';
        $where['so.status'] = 2;
        $query['status'] = 2;
        $adminList = Sellorder::alias('so')
                ->join('store s', 'so.store_id = s.id')
                ->join('selladmin sa', 'so.sell_id = sa.id')
                ->where($where)
                ->field('so.*,s.store_name,sa.admin_note')
                ->paginate(12)
                ->appends($query);
        $sellordermoney = Sellorder::alias('so')
                ->join('store s', 'so.store_id = s.id')
                ->join('selladmin sa', 'so.sell_id = sa.id')
                ->where($where)
                ->sum('enter_money');
        return view('entermoneylist', [
            'AdminList' => $adminList,
            'store_name' => $store_name,
            'store_phone' => $store_phone,
            'ordnum' => $ordnum,
            'selladmin' => $selladmin1,
            'adminid' => $adminid,
            'sellordermoney' => $sellordermoney,
        ]);
    }
    
    
    /*
     * 管理费列表
     * author xfwed
     * date 2019.01.22
     */

    public function serveMoneyList() {
        $id = session('S_ID');
        $row = Selladmin::where('id', '=', $id)->field('id,level')->find();
        $level = $row['level'];
        if ($level == 0 || $level == 1) {
            $where1['level'] = 2;
        } elseif ($level == 2) {
            $where1['pid'] = $id;
        } elseif ($level == 3) {
            $where1['id'] = $id;
        }
        $selladmin1 = Selladmin::where($where1)->where('status', '=', 1)->select();


        $store_name = input('param.store_name');
        $store_phone = input('param.store_phone');
        $ordnum = input('param.ordnum');
        $adminid = input('param.adminid');
        if ($store_phone) {
            $where['s.store_phone'] = $store_phone;
            $query['store_phone'] = $store_phone;
        }

        if ($store_name) {
            $where['s.store_name'] = array('like', '%' . $store_name . '%');
            $query['store_name'] = $store_name;
        }

        if ($ordnum) {
            $where['so.ord_num'] = $ordnum;
            $query['ordnum'] = $ordnum;
        }

        $ctm_id = session('S_ID');
        $selladmin = Selladmin::where('id', '=', $ctm_id)->field('id,level')->find();
        $level = $selladmin['level'];
        if ($level == 0 or $level == 1) {//超级管理员和总经理
            if ($adminid) {
                $selladminrow = Selladmin::where('id', '=', $adminid)->field('id,level')->find();
                if ($selladminrow['level'] == 2) {
                    $ids = Selladmin::where('pid', '=', $adminid)->column('id');
                    $where['so.sell_id'] = ['in', $ids];
                } elseif ($selladminrow['level'] == 3) {
                    $where['so.sell_id'] = $adminid;
                }
                $query['adminid'] = $adminid;
            }
        } elseif ($level == 2) {//经理
            if ($adminid) {
                $where['so.sell_id'] = $adminid;
                $query['adminid'] = $adminid;
            } else {
                $ids = Selladmin::where('pid', '=', $ctm_id)->column('id');
                $where['so.sell_id'] = ['in', $ids];
            }
        } elseif ($level == 3) {//员工
            $where['so.sell_id'] = $ctm_id;
        }

        $where['so.type'] = ['in', '1,2,4'];
        $query['type'] = '1,2,3';
        $where['so.status'] = 2;
        $query['status'] = 2;
        $adminList = Sellorder::alias('so')
                ->join('store s', 'so.store_id = s.id')
                ->join('selladmin sa', 'so.sell_id = sa.id')
                ->where($where)
                ->field('so.*,s.store_name,sa.admin_note')
                ->paginate(12)
                ->appends($query);
        $sellordermoney = Sellorder::alias('so')
                ->join('store s', 'so.store_id = s.id')
                ->join('selladmin sa', 'so.sell_id = sa.id')
                ->where($where)
                ->sum('serve_money');
        return view('servemoneylist', [
            'AdminList' => $adminList,
            'store_name' => $store_name,
            'store_phone' => $store_phone,
            'ordnum' => $ordnum,
            'selladmin' => $selladmin1,
            'adminid' => $adminid,
            'sellordermoney' => $sellordermoney,
        ]);
    }

}
